package com.sskj.fca.ui.fragment;


import android.annotation.SuppressLint;
import android.text.Html;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.jakewharton.rxbinding2.widget.RxTextView;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.NumberUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseFragment;
import com.sskj.lib.bean.UserBean;
import com.sskj.fca.R;
import com.sskj.fca.bean.BankersMainBean;
import com.sskj.fca.bean.CommonBean;
import com.sskj.fca.presenter.BankerJackpotHoldFragmentPresenter;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import io.reactivex.android.schedulers.AndroidSchedulers;

@Route(path = RConfig.APP_FRAGMENT_BANKER_JACKPOT_HOLD)//坐庄
public class BankerJackpotHoldFragment extends BaseFragment<BankerJackpotHoldFragmentPresenter> {
    @BindView(R.id.tv_hold0)
    TextView tvHold0;
    @BindView(R.id.tv_hold1)
    TextView tvHold1;
    @Autowired
    String moneyType;
    @BindView(R.id.tv_wallet_balance)
    TextView tvWalletBalance;
    @BindView(R.id.et_money)
    EditText etMoney;
    @BindView(R.id.tv_money_type)
    TextView tvMoneyType;
    @BindView(R.id.tv_stock_now)
    TextView tvStockNow;
    @BindView(R.id.tv_stock_after)
    TextView tvStockAfter;
    @BindView(R.id.tv_banker_fund_now)
    TextView tvBankerFundNow;
    @BindView(R.id.tv_banker_fund_after)
    TextView tvBankerFundAfter;
    @BindView(R.id.tv_xishi)
    TextView tv_xishi;
    @BindView(R.id.btn_in_out)
    TextView btn_in_out;

    private String xiShiRate = "0";
    private String nowPoll = "0";
    private String totalPoll = "0";

    @Override
    protected int getLayoutId() {
        return R.layout.app_fragment_banker_jackpot_hold;
    }

    @Override
    public BankerJackpotHoldFragmentPresenter getPresenter() {
        return new BankerJackpotHoldFragmentPresenter();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            mPresenter.getCommonData();
            mPresenter.getUserInfo();
            mPresenter.getBankerPoolMainMsg(moneyType.equals("JC") ? "pb" : "cny");
        }
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void initView() {
        ARouter.getInstance().inject(this);
        etMoney.setText("0");
        mPresenter.getCommonData();
        mPresenter.getUserInfo();
        mPresenter.getBankerPoolMainMsg(moneyType.equals("JC") ? "pb" : "cny");
        tvMoneyType.setText(moneyType);
        tvHold0.setText(Html.fromHtml("注入请填写 <font color=\"#57BD8B\">正值</font>，抽取请填写 <font color=\"#57BD8B\">负值</font>，" +
                "请保证额度在您的承受范围内。"));
        tvHold1.setText(Html.fromHtml("注入金额的 <font color=\"#57BD8B\">2%</font> 将作为不可退还的股份稀释费扣除（其他庄家瓜分稀释费），此费用将从您的资金中扣除。"));

        RxTextView.textChanges(etMoney)
                .debounce(100, TimeUnit.MILLISECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .map(CharSequence::toString)
                .subscribe(s -> {
                    if (TextUtils.isEmpty(s)) {
                        tvStockAfter.setText("0.00%");
                        tvBankerFundAfter.setText(nowPoll + " " + moneyType);
                        tv_xishi.setText("0" + moneyType);
                        return;
                    }
                    BigDecimal xishiRate = new BigDecimal(xiShiRate);
                    BigDecimal nowpoll = new BigDecimal(nowPoll);
                    BigDecimal totalpoll = new BigDecimal(totalPoll);
                    try {
                        Double aDouble = Double.valueOf(s);
                        if (s.contains("-")) {
                            BigDecimal num = new BigDecimal(s.substring(1, s.length()));
                            if (Objects.equals(num, BigDecimal.valueOf(0))) {
                                tvStockAfter.setText("0.00%");
                                tvBankerFundAfter.setText(nowPoll + " " + moneyType);
                                tv_xishi.setText("0" + moneyType);
                            }
                            if (totalpoll.toString().equals(num.toString())) {
                                tvStockAfter.setText("0.00%");
                            } else {
                                tvStockAfter.setText(NumberUtil.keep2(String.valueOf(
                                        nowpoll.subtract(num).divide(totalpoll.subtract(num), 4, RoundingMode.HALF_UP).multiply(BigDecimal.valueOf(100)))) + "%");
                            }
                            tvBankerFundAfter.setText(nowpoll.subtract(num.subtract(num.multiply(xishiRate).divide(BigDecimal.valueOf(100), BigDecimal.ROUND_CEILING))) + " " + moneyType);
                            tv_xishi.setText(num.multiply(xishiRate).divide(BigDecimal.valueOf(100), BigDecimal.ROUND_CEILING) + " " + moneyType);
                        } else {
                            BigDecimal num = new BigDecimal(s);
                            if (Objects.equals(num, BigDecimal.valueOf(0))) {
                                tvStockAfter.setText("0.00%");
                                tvBankerFundAfter.setText(nowPoll + " " + moneyType);
                                tv_xishi.setText("0" + moneyType);
                            }
                            if (String.valueOf(totalpoll.add(num)).equals("0")) {
                                tvStockAfter.setText("0.00%");
                            } else {
                                tvStockAfter.setText(NumberUtil.keep2(String.valueOf((nowpoll.add(num).divide(totalpoll.add(num), 4, RoundingMode.HALF_UP)).multiply(BigDecimal.valueOf(100)))) + "%");
                            }
                            tvBankerFundAfter.setText(nowpoll.add(num.subtract(num.multiply(xishiRate).divide(BigDecimal.valueOf(100), BigDecimal.ROUND_CEILING))) + " " + moneyType);
                            tv_xishi.setText(num.multiply(xishiRate).divide(BigDecimal.valueOf(100), BigDecimal.ROUND_CEILING) + " " + moneyType);
                        }

                    } catch (NumberFormatException e) {
                        e.printStackTrace();
                        return;
                    }
                });

        ClickUtil.click(btn_in_out, () -> {
            if (TextUtils.isEmpty(etMoney.getText().toString())) {
                ToastUtil.showShort("请输入金额");
                return;
            }
            mPresenter.bankerInOut(moneyType.equals("JC") ? "pb" : "cny", etMoney.getText().toString().trim());
        });

    }

    public void bankeInOutSuccess() {
        etMoney.setText("");
        mPresenter.getCommonData();
        mPresenter.getUserInfo();
        mPresenter.getBankerPoolMainMsg(moneyType.equals("JC") ? "pb" : "cny");
    }

    public void updataUi(BankersMainBean bankersMainBean) {
        totalPoll = bankersMainBean.getBank_money_total();
        nowPoll = bankersMainBean.getBank_money();
        tvBankerFundNow.setText(NumberUtil.keep2(bankersMainBean.getBank_money()) + " " + moneyType);
        tvStockNow.setText(NumberUtil.keep2(Float.valueOf(bankersMainBean.getScale()) * 100) + "%");
    }

    public void updateUser(UserBean userBean) {
        if (moneyType.equals("JC")) {
            tvWalletBalance.setText(userBean.getBalance_pb() + " " + moneyType);
        }
        if (moneyType.equals("USDT")) {
            tvWalletBalance.setText(userBean.getBalance_cny() + " " + moneyType);
        }
    }

    public void updataCommonData(CommonBean commonBean) {
        xiShiRate = commonBean.getBuy_info().getXishi_rate().getXishi_rate();
        tvHold1.setText(Html.fromHtml("注入金额的 <font color=\"#57BD8B\">"
                + commonBean.getBuy_info().getXishi_rate().getXishi_rate()
                + "%</font> 将作为不可退还的股份稀释费扣除（其他庄家瓜分稀释费），此费用将从您的资金中扣除。"));
    }


}
